package com.example.todolist;

import java.util.ArrayList;

import android.app.FragmentManager;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.drm.DrmStore.Action;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.example.todolist.NewItemFragment.OnNewItemAddedListener;
import com.example.todolist.adapter.ToDoItemAdapter;
import com.example.todolist.entity.ToDoItem;
import com.example.todolist.provider.ToDoContentProvider;
import com.example.todolist.utils.StringUtils;

public class ToDoListActivity extends BaseActivity implements OnNewItemAddedListener, LoaderCallbacks<Cursor> {

	private FragmentManager fm;
	private ToDoListFragment toDoListFragment;
	
	private ArrayList<ToDoItem> todoItems;
	private ToDoItemAdapter aa;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initValues();
        findViews();
        bindEvents();
        
        LoaderManager loaderManager = getLoaderManager();
        loaderManager.initLoader(0, null, this);
    }

	@Override
	public void initValues() {
		todoItems = new ArrayList<ToDoItem>();
		aa = new ToDoItemAdapter(this, R.layout.todolist_item, todoItems);
		
		fm = getFragmentManager();
	}

	@Override
	public void findViews() {
		toDoListFragment = (ToDoListFragment) fm.findFragmentById(R.id.ToDoListFragment);
		
	}

	@Override
	public void bindEvents() {
		toDoListFragment.setListAdapter(aa);
//		toDoListFragment.getListView().setOnItemClickListener(new OnItemClickListener() {
//
//			@Override
//			public void onItemClick(AdapterView<?> parent, View view,
//					int position, long id) {
//				Intent intent = new Intent(Intent.ACTION_VIEW);
//				intent.setData(ContentUris.withAppendedId(ToDoContentProvider.CONTENT_URI, id));
//				startActivity(intent);
//				
//			}
//		});
	}

	@Override
	public void onNewItemAdded(String newItem) {
		if(StringUtils.isNullOrEmpty(newItem)) {
			return;
		}
		
		ContentValues values = new ContentValues();
		values.put(ToDoContentProvider.KEY_TASK, newItem);
		getContentResolver().insert(ToDoContentProvider.CONTENT_URI, values);
		
		getLoaderManager().restartLoader(0, null, this);
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		System.out.println("onCreate:" + Thread.currentThread().getName());
		CursorLoader cursorLoader = new CursorLoader(this, ToDoContentProvider.CONTENT_URI, null, null, null, ToDoContentProvider.KEY_ID + " desc");
		return cursorLoader;
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		System.out.println("onFinished:" + Thread.currentThread().getName());
		int taskIndex = data.getColumnIndexOrThrow(ToDoContentProvider.KEY_TASK);
		
		todoItems.clear();
		while(data.moveToNext()) {
			ToDoItem toDoItem = new ToDoItem(data.getString(taskIndex));
			todoItems.add(toDoItem);
		}
		aa.notifyDataSetChanged();
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		System.out.println("onReset:" + Thread.currentThread().getName());
	}


}
